import threading
import tkinter as tk
import webbrowser
from tkinter import scrolledtext

import requests
from bs4 import BeautifulSoup


class MovieSpider:
    def __init__(self):
        self.url = 'https://www.dytt8.net/html/gndy/dyzz/list_23_{}.html'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
        self.movies = []
        self.index = 1

    def get_movies(self):
        self.movies = []
        self.index = 1
        url = self.url.format(self.index)
        response = requests.get(url, headers=self.headers)
        response.encoding = 'gb2312'
        soup = BeautifulSoup(response.text, 'html.parser')
        movie_list = soup.find_all('div', class_='co_content8')[0].ul.find_all('table')
        for movie in movie_list:
            title = movie.a.text
            link = 'https://www.dytt8.net' + movie.a['href']
            self.movies.append({'title': title, 'link': link})

    def refresh_movies(self):
        self.index = self.index + 1
        url = self.url.format(self.index)
        response = requests.get(url, headers=self.headers)
        response.encoding = 'gb2312'
        soup = BeautifulSoup(response.text, 'html.parser')
        movie_list = soup.find_all('div', class_='co_content8')[0].ul.find_all('table')
        for movie in movie_list:
            title = movie.a.text
            link = 'https://www.dytt8.net' + movie.a['href']
            self.movies.append({'title': title, 'link': link})

class MovieUI:
    def __init__(self):
        self.spider = MovieSpider()
        self.window = tk.Tk()
        self.window.title('电影天堂')
        self.start_button = tk.Button(self.window, text='开始爬虫', font=('Arial', 16), command=self.start_spider)
        self.start_button.pack(pady=10)
        self.refresh_button = tk.Button(self.window, text='刷新数据', font=('Arial', 16), command=self.refresh_movies)
        self.refresh_button.pack(pady=10)
        self.movies_text = scrolledtext.ScrolledText(self.window, width=80, height=20, font=('Arial', 14))
        self.movies_text.pack(pady=10)

    def start_spider(self):
        self.movies_text.insert(tk.END, '正在爬取电影信息，请稍等...\n')
        self.start_button.config(state='disabled')
        self.refresh_button.config(state='disabled')
        t = threading.Thread(target=self.spider.get_movies)
        t.setDaemon(True)
        t.start()
        t.join()
        self.window.after(100, self.show_movies)

    def refresh_movies(self):
        self.movies_text.insert(tk.END, '正在刷新电影信息，请稍等...\n')
        self.refresh_button.config(state='disabled')
        t = threading.Thread(target=self.spider.refresh_movies)
        t.setDaemon(True)
        t.start()
        t.join()
        self.window.after(100, self.show_movies)

    def show_movies(self):
        # if len(self.spider.movies) < 10:
        #     self.window.after(100, self.show_movies)
        # else:
        self.start_button.config(state='normal')
        self.refresh_button.config(state='normal')
        self.movies_text.delete('1.0', tk.END)
        length = len(self.spider.movies)
        for i in range(length):
            movie = self.spider.movies[i]
            self.movies_text.insert(tk.END, '{}. {}\n'.format(i + 1, movie['title']))
            self.movies_text.insert(tk.END, '{}\n\n'.format(movie['link']))
            self.movies_text.tag_add('link', '{}.{}'.format(i + 1, len(str(i + 1)) + 2),
                                     '{}.{}'.format(i + 1, len(str(i + 1)) + 2 + len(movie['link'])))
            self.movies_text.tag_config('link', foreground='blue', underline=True)
            self.movies_text.bind('<Button-1>', self.open_link)

    def open_link(self, event):
        index = self.movies_text.index(tk.CURRENT).split('.')[0]
        movie = self.spider.movies[int(index) - 1]
        webbrowser.open(movie['link'])


if __name__ == '__main__':
    ui = MovieUI()
    ui.window.mainloop()
